package cn.itcast.star.graph.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author Windows
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpServletRequest request;

    @SneakyThrows
    public boolean isExclude() {
        try {
            String requestURI = request.getRequestURI();
            if(requestURI.startsWith("/member")){
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime",LocalDateTime.class, LocalDateTime.now());
//        if(isExclude()){
//            this.strictInsertFill(metaObject, "createBy", String.class, loadUserId() + "");
//        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.class, metaObject);
//        if(isExclude()){
//            this.setFieldValByName("updateBy", loadUserId() + "", metaObject);
//        }
    }
    /**
     * 获取当前登录人的ID
     *
     * @return
     */
    private static Long loadUserId() {

        //获取当前登录人的id
//        try {
//            LoginUser = SecurityUtils.getLoginUser();
//            if (ObjectUtils.isNotEmpty(loginUser)) {
//                return loginUser.getUserId();
//            }
//            return 1L;
//        } catch (Exception e) {
//            return 1L;
//        }
        return 1L;
    }
}
